<?php

class MallgoodsController extends Controller
{
    /*
    * 获取列表分页
    *
    */
    public function actionGetListPaging()
    {

        try {
            //参数验证

            //检测
            $user_id = SessionService::getUserIDBySessionid();


            $searchsql = Util::initSearchSql();

            $searchsql = Util::getSerarchStr($searchsql,"type_id","type_id");
            $searchsql = Util::getSerarchStr($searchsql,"material_id","material_id");
            $searchsql = Util::getSerarchStr($searchsql,"shop_id","shop_id");
            $searchsql = Util::getSerarchStr($searchsql,"brands_id","brands_id");


            //获取count
            $totalcount = MallGoods::model()->count($searchsql);

            //排序
            $searchsql = $searchsql . " order by create_time desc";

            //加上分页控制
            $searchsql = Util::addLimitSql($searchsql);

            //获取list
            $list = MallGoods::model()->findAll($searchsql);


            Util::responseList($totalcount, $list);

        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }


    }


    /*
    * 添加
    *
    */
    public function actionAdd()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $name = Util::getParamNotEmpty("name");
            $name_short = Util::getParamNotEmpty("name_short");
            $type_id = Util::getParamNotEmpty("type_id");
            $material_id = Util::getParamNotEmpty("material_id");
            $shop_id = Util::getParamNotEmpty("shop_id");
            $brands_id = Util::getParamNotEmpty("brands_id");
            $detail_info = Util::getParamNotEmpty("detail_info");
            $pic_detail = Util::getParamNotEmpty("pic_detail");



            //添加
            $obj = new MallGoods();
            Util::initObjData($obj);


            $obj->name = $name;
            $obj->name_short = $name_short;
            $obj->type_id = $type_id;
            $obj->material_id = $material_id;
            $obj->shop_id = $shop_id;
            $obj->brands_id = $brands_id;
            $obj->detail_info = $detail_info;
            $obj->pic_detail = $pic_detail;


            Util::saveObjData($obj);


            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 修改
    *
    */
    public function actionUpdate()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");
            $name = Util::getParamNotEmpty("name");
            $name_short = Util::getParamNotEmpty("name_short");
            $type_id = Util::getParamNotEmpty("type_id");
            $material_id = Util::getParamNotEmpty("material_id");
            $shop_id = Util::getParamNotEmpty("shop_id");
            $brands_id = Util::getParamNotEmpty("brands_id");
            $detail_info = Util::getParamNotEmpty("detail_info");
            $pic_detail = Util::getParamNotEmpty("pic_detail");


            //验证存在
            $obj = MallGoods::model()->findByPK($obj_id);

            Util::checkExistAndNotDelete($obj);

            //修改信息

            $obj->name = $name;
            $obj->name_short = $name_short;
            $obj->type_id = $type_id;
            $obj->material_id = $material_id;
            $obj->shop_id = $shop_id;
            $obj->brands_id = $brands_id;
            $obj->detail_info = $detail_info;
            $obj->pic_detail = $pic_detail;

            //保存
            Util::saveObjData($obj);



            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 删除
    */
    public function actionDelete()
    {

        try {
            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //参数验证
            $obj_id = Util::getParamNotEmpty("obj_id");


            //删除
            $obj = MallGoods::model()->findByPK($obj_id);

            Util::checkExistAndNotDelete($obj);

            Util::deleteObjData($obj);

            Util::saveObjData($obj);



            //返回数据
            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        } catch (Exception $e) {
            Util::responseErrorMsg($e->getMessage());

        }
    }
}